package subject_set.interview.Day1_10_kujiale.tran;

import subject_set.interview.Day1_10_kujiale.tran.object.User;

import java.io.*;
import java.util.*;

/**
 * @author haomin
 * @date 2023/01/10 16:15
 **/
public class Main {
    public static Set<User> maiPiao = new HashSet<>();
    public static Set<User> jianPiao = new HashSet<>();

    // 初始化工作，可以进行文件操作，读取购票用户信息和已检票用户信息
    public static void create() {
        maiPiao.add(new User("张三", "31000019900011"));
        maiPiao.add(new User("李四", "32000019882222"));
    }

    // 1.检票
    private static void fun1() {
        Scanner in = new Scanner(System.in);
        System.out.println("现在开始检票，请输入姓名，身份证号：（并用空格分开）");
        String message = in.nextLine();

        String[] userMessage = message.split(" ");
        if(userMessage.length <= 0 || userMessage.length > 2) {
            System.out.println("你的输入有误，请重新操作！");
            return;
        }

        User curUser = new User(userMessage[0], userMessage[1]);
        if(maiPiao.contains(curUser)) {
            // 删除检票的用户信息
            maiPiao.remove(curUser);
            // 增加已检票的用户信息
            jianPiao.add(curUser);

            System.out.println("检票已完成");
        }else {
            System.out.println("您未购票");
        }
    }

    // 2.买票
    private static void fun2() {
        Scanner in = new Scanner(System.in);
        System.out.println("现在开始买票，请输入姓名，身份证号：（并用空格分开）");
        String message = in.nextLine();

        String[] userMessage = message.split(" ");
        if(userMessage.length <= 0 || userMessage.length > 2) {
            System.out.println("你的输入有误，请重新操作！");
            return;
        }

        User curUser = new User(userMessage[0], userMessage[1]);
        if(maiPiao.contains(curUser)) {
            System.out.println("您已经购票，请勿重复购票！");
        }else {
            maiPiao.add(curUser);
            System.out.println("购票成功");
        }
    }

    // 3.退票
    private static void fun3() {
        Scanner in = new Scanner(System.in);
        System.out.println("现在开始退票，请输入姓名，身份证号：（并用空格分开）");
        String message = in.nextLine();

        String[] userMessage = message.split(" ");
        if(userMessage.length <= 0 || userMessage.length > 2) {
            System.out.println("你的输入有误，请重新操作！");
            return;
        }

        User curUser = new User(userMessage[0], userMessage[1]);
        if(maiPiao.contains(curUser)) {
            maiPiao.remove(curUser);
            System.out.println("退票成功！");
        }else if(jianPiao.contains(curUser)) {
                jianPiao.remove(curUser);
                System.out.println("退票成功！");
        } else {
            System.out.println("不好意思，您未购票，无法退票");
        }
    }


    public static void main(String[] args) {
        create();
        while (true) {
            System.out.println("==========================");
            System.out.println("+  1.检票                 +");
            System.out.println("+  2.买票(buy操作)         +");
            System.out.println("+  3.退票(refund操作)      +");
            System.out.println("+  4.退出系统              +");
            System.out.println("==========================");
            System.out.print("请输入您想做的操作的序号：");
            Scanner in = new Scanner(System.in);
            int i = in.nextInt();
            switch (i) {
                case 1:
                    fun1();
                    break;
                case 2:
                    fun2();
                    break;
                case 3:
                    fun3();
                    break;
                case 4:
                    return;
                default:
                    System.out.println("您的操作有误，请重新输入（对应的序号）！");
            }
            System.out.println();
        }
    }
}